package com.iims.market.service.impl;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.iims.common.exception.CustomException;
import com.iims.market.domain.Contract;
import com.iims.market.domain.PurposeContract;
import com.iims.market.mapper.ContractMapper;
import com.iims.market.mapper.PurposeContractMapper;
import com.iims.market.service.IContractCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/6/18 11:25
 */
@Service
public class ContractCommonServiceImpl implements IContractCommonService {

    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private PurposeContractMapper purposeContractMapper;

    @Override
    public void checkNameAndCodeUnique(Long ctraId, String ctraName, String ctraCode) {
        if (!contractNameUnique(
                ctraId, ctraName)) {
            throw new CustomException("新增合同失败,合同名称" + ctraName + "已存在");
        }
        if (!contractCodeUnique(
                ctraId, ctraCode)) {
            throw new CustomException("新增合同失败,合同编号" + ctraCode + "已存在");
        }
    }

    @Override
    public boolean contractNameUnique(Long ctraId, String ctraName) {
        Long id = getId(ctraId);
        List<Contract> infos = contractMapper.selectList(
                new LambdaQueryWrapper<Contract>().eq(Contract::getContractName, ctraName));
        Contract SingResult = null;
        if (infos.size() > 0) {
            SingResult = infos.get(0);
            if (Validator.isNotNull(SingResult)
                    && SingResult.getContractId().longValue() != id.longValue()) {
                return false;
            }
        }

        List<PurposeContract> pinfos = purposeContractMapper.selectList(
                new LambdaQueryWrapper<PurposeContract>().eq(PurposeContract::getContractName, ctraName));
        PurposeContract pcSingResult = null;
        if (pinfos.size() > 0) {
            pcSingResult = pinfos.get(0);
            if (Validator.isNotNull(pcSingResult)
                    && pcSingResult.getPurposeContractId().longValue() != id.longValue()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean contractCodeUnique(Long ctraId, String ctraCode) {
        Long id = getId(ctraId);
        List<Contract> infos = contractMapper.selectList(
                new LambdaQueryWrapper<Contract>().eq(Contract::getContractCode, ctraCode));
        Contract SingResult = null;
        if (infos.size() > 0) {
            SingResult = infos.get(0);
            if (Validator.isNotNull(SingResult)
                    && SingResult.getContractId().longValue() != id.longValue()) {
                return false;
            }
        }

        List<PurposeContract> pinfos = purposeContractMapper.selectList(
                new LambdaQueryWrapper<PurposeContract>().eq(PurposeContract::getPcontractCode, ctraCode));
        PurposeContract pcSingResult = null;
        if (pinfos.size() > 0) {
            pcSingResult = pinfos.get(0);
            if (Validator.isNotNull(pcSingResult)
                    && pcSingResult.getPurposeContractId().longValue() != id.longValue()) {
                return false;
            }
        }
        return true;
    }

    private Long getId(Long ctraId) {
        return Validator.isNull(ctraId) ? -1L : ctraId;
    }
}
